<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/4/17
 * Time: 18:22
 */

namespace app\home\controller;


class Release extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->assign('action',strtolower(request()->action()));
    }
    public function buy(){
        $this->setSeo(['title'=>'发布求购信息']);
        return $this->fetch();
    }
    public function rent(){
        $this->setSeo(['title'=>'发布求租信息']);
        return $this->fetch();
    }
    public function entrustUsed(){
        $this->setSeo(['title'=>'委托出售']);
        return $this->fetch();
    }
    public function entrustRental(){
        $this->setSeo(['title'=>'委托出租']);
        return $this->fetch();
    }
    public function entrustRentalDo(){
        $return['status'] = 0;
        $verfiy = input('post.verfiy');
        if($verfiy){
            if(!captcha_check($verfiy)){
                return json(['status'=>0,'msg'=>'验证码不正确']);
            }
        }
        if($this->add(model('rental_entrust'))){
            $return['status'] = 1;
            $return['msg'] = '委托出租信息发布成功！';
        }else{
            $return['msg'] = '委托出租信息发布失败';
        }
        return json($return);
    }
    public function entrustUsedDo(){
        $return['status'] = 0;
        $verfiy = input('post.verfiy');
        if($verfiy){
            if(!captcha_check($verfiy)){
                return json(['status'=>0,'msg'=>'验证码不正确']);
            }
        }
        if($this->add(model('second_entrust'))){
            $return['status'] = 1;
            $return['msg'] = '委托出售信息发布成功！';
        }else{
            $return['msg'] = '委托出售信息发布失败';
        }
        return json($return);
    }
    public function rentDo(){
        $return['status'] = 0;
        $verfiy = input('post.verfiy');
        if($verfiy){
            if(!captcha_check($verfiy)){
                return json(['status'=>0,'msg'=>'验证码不正确']);
            }
        }
        if($this->add(model('rental_buy'))){
            $return['status'] = 1;
            $return['msg'] = '求租信息发布成功！';
        }else{
            $return['msg'] = '求租信息发布失败';
        }
        return json($return);
    }
    public function buyDo(){
        $return['status'] = 0;
        $verfiy = input('post.verfiy');
        if($verfiy){
            if(!captcha_check($verfiy)){
                return json(['status'=>0,'msg'=>'验证码不正确']);
            }
        }
        if($this->add(model('second_buy'))){
            $return['status'] = 1;
            $return['msg'] = '求购信息发布成功！';
        }else{
            $return['msg'] = '求购信息发布失败';
        }
        return json($return);
    }
    private function add($obj){
        $data = input('post.data/a');
        if($this->userInfo){
            $data['user_id'] = $this->userInfo['id'];
        }
        $area = explode(',',$data['area']);
        $data['city'] = $area[0];
        $data['area'] = isset($area[1]) ? $area[1] : 0;
        $house_type   = input('post.house_type/a');
        $chaoxiang    = input('post.chaoxiang');
        $zhuangxiu    = input('post.zhuangxiu');
        $rental_type  = input('post.rental_type');
        $rental_pay   = input('post.rental_pay');
        $house_type && $data['house_type'] = $house_type;
        $chaoxiang  && $data['chaoxiang']  = $chaoxiang;
        $zhuangxiu  && $data['zhuangxiu']  = $zhuangxiu;
        $rental_type && $data['rental_type'] = $rental_type;
        $rental_pay && $data['rental_pay']   = $rental_pay;
        return $obj->allowField(true)->save($data);
    }
}